package top.huiwow.commands;


import org.bukkit.Bukkit;
import org.bukkit.command.*;
import org.bukkit.entity.Player;
import top.huiwow.GameCore;
import top.huiwow.managers.GamePlayer;
import top.huiwow.managers.GamePlayerManager;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class PlayerStatCommand implements CommandExecutor, TabCompleter {
    GameCore plugin;
    GamePlayerManager gamePlayerManager;
    List<String> groupnames;

    public PlayerStatCommand() {
        plugin = GameCore.getInstance();
        gamePlayerManager = plugin.getGamePlayerManager();
        groupnames = new ArrayList<>();
        groupnames.add("speed");
        groupnames.add("strength");
        groupnames.add("defense");
        groupnames.add("cc");
        groupnames.add("cd");
        groupnames.add("mana");
        groupnames.add("intelligence");
        groupnames.add("hp");
        groupnames.add("maxhp");
    }

    @Override
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
        if (sender instanceof ConsoleCommandSender || sender instanceof Player) {
            if (args.length == 1) {

                //this is used to check player stat
                Player p = null;
                try {
                    p = Bukkit.getPlayer(args[0]);
                } catch (Exception e) {
                    //
                }
                if (p == null) {
                    sender.sendMessage("Cannot find the Player!");
                } else {
                    sender.sendMessage(gamePlayerManager.get(p.getUniqueId()).printPlayerStats());
                }
            } else if (args.length == 3) {
                if (sender instanceof Player && !sender.hasPermission("akaishi.admin")) {
                    sender.sendMessage("YOU DONT HAVE ENOUGH PERMISSION TO DO THIS!");
                    return true;
                }
                //modify player stat
                Player p = null;
                try {
                    p = Bukkit.getPlayer(args[0]);
                } catch (Exception e) {
                    //
                }
                if (p == null) {
                    sender.sendMessage("Cannot find the Player!");
                } else {
                    String toModify = args[1].toLowerCase();
                    switch (toModify) {
                        case "strength":
                        case "intelligence":
                        case "mana":
                        case "cc":
                        case "cd":
                        case "defense":
                        case "hp":
                        case "maxhp":
                        case "speed": {
                            Integer amount = null;
                            try {
                                amount = Integer.parseInt(args[2]);
                            } catch (NumberFormatException e) {
                                sender.sendMessage("NAN");
                                return true;
                            }
                            GamePlayer gamePlayer = gamePlayerManager.get(p.getUniqueId());
                            switch (toModify) {
                                case "strength":
                                    gamePlayer.setStrength(amount);
                                    break;
                                case "intelligence":
                                    gamePlayer.setIntelligence(amount);
                                    break;
                                case "mana":
                                    gamePlayer.setMana(amount);
                                    break;
                                case "cc":
                                    gamePlayer.setCritChance(amount);
                                    break;
                                case "cd":
                                    gamePlayer.setCritDamage(amount);
                                    break;
                                case "defense":
                                    gamePlayer.setDefense(amount);
                                    break;
                                case "speed":
                                    gamePlayer.setSpeed(amount);
                                    break;
                                case "hp":
                                    gamePlayer.getPlayer().setHealth(Math.min(gamePlayer.getPlayer().getMaxHealth(), amount));
                                    break;
                                case "maxhp":
                                    gamePlayer.getPlayer().setMaxHealth(amount);
                                    break;
                            }
                            gamePlayer.controlledStats = true;
                            sender.sendMessage("successful changed your stats!Your stats are now controlled and cannot changed by items.Use /uncontrolstats <player> to un-control.");
                            break;
                        }
                        default:
                            sender.sendMessage("available stats: " + Arrays.toString(groupnames.toArray()));
                            break;
                    }
                }
            } else if (sender instanceof Player && args.length == 0) {
                Player player = ((Player) sender);
                //my stat
                player.sendMessage(gamePlayerManager.get(player.getUniqueId()).printPlayerStats());

            } else {
                sender.sendMessage("Invalid usage.Correct usage:\n" +
                        "/playerstat\n" +
                        "/playerstat <player>\n" +
                        "/playerstat <player> <stat> <value>");
            }
        }

        return true;
    }

    @Override
    public List<String> onTabComplete(CommandSender sender, Command cmd, String label, String[] args) {
        if (args.length != 2) return null;

        String arg = args[1];

        if (!isEmptyTab(arg)) {
            return autofill(groupnames, arg);
        }

        return groupnames;
    }

    private boolean isEmptyTab(String s) {
        return s == null || s.equals(" ") || s.isEmpty();
    }

    private List<String> autofill(List<String> groupnames, String input) {
        List<String> list = new ArrayList<>();

        for (String entry : groupnames) {
            if (entry.length() >= input.length()) {
                if (input.equalsIgnoreCase(entry.substring(0, input.length()))) {
                    list.add(entry);
                }
            }
        }

        if (list.isEmpty()) {
            return groupnames;
        }

        return list;
    }
}
